package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.CurtainContract;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;

/**
 * Created by sunlg on 2017/5/3.
 */

public class CurtainPresenter implements CurtainContract.CurtainPresenter {

    private CurtainContract.CurtainView mView;
    private SHServiceWrapper mSHServiceWrapper;

    public CurtainPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void unfold(int curtainId, int level, int speed) {
        mSHServiceWrapper.unfoldCurtain(curtainId, level, speed, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.unfoldSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void fold(int curtainId, int level, int speed) {
        mSHServiceWrapper.foldCurtain(curtainId, level, speed, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.foldSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void stop(int curtainId) {
        mSHServiceWrapper.stopCurtain(curtainId, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.stopSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(CurtainContract.CurtainView view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }
}
